package cn.shop.dys.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.bean.Msg;
import cn.shop.lib_common.CommonViewHolder;

/**
 * Created by jian on 2016/4/10.
 */
public class ChatAdapter extends BaseAdapter{
    private Context mContext;
    private List<Msg> mChatInfos;

    public ChatAdapter(Context mContext, List<Msg> mChatInfos) {
        this.mContext = mContext;
        this.mChatInfos = mChatInfos;
    }

    @Override
    public int getCount() {
        return mChatInfos==null?0:mChatInfos.size();
    }

    @Override
    public Msg getItem(int position) {
        return mChatInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        return mChatInfos.get(position).type;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Msg msg = mChatInfos.get(position);
        int type =getItemViewType(position);
        CommonViewHolder holder=null;
        int layoutRes=-1;
        switch (type){
            case Msg.CHAT_TYPE_OTHER:
                layoutRes= R.layout.item_chat_receive;
                break;
            case Msg.CHAT_TYPE_ME:
                layoutRes= R.layout.item_chat_send;
                break;
        }
        holder=CommonViewHolder.getCommonViewHolder(convertView,mContext,layoutRes);
        holder.getTv(R.id.content).setText(msg.content);
        holder.getTv(R.id.time).setText(msg.time);
        return holder.convertView;
    }
}
